/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package foalvleditor.core.impl;

import foalvleditor.ctrl.BaseController;
import foalvleditor.gameobjects.AbstractGameObject;
import foalvleditor.gameobjects.Bitmap;
import foalvleditor.gameobjects.GameEvent;
import foalvleditor.gui.FoaLvlEditorView;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Mikael
 */
public class LevelModel {

    private ArrayList<AbstractGameObject> level=
         new ArrayList<AbstractGameObject>();
    private boolean changed=false;
    private ArrayList<GameEvent> events=new ArrayList<GameEvent>();
    private String name="(Untitled)";
    private AbstractGameObject background;
    private FoaLvlEditorView view;

    private static LevelModel instance=new LevelModel();

    public static LevelModel getInstance(){
        return instance;
    }

    public boolean readyToClose(){
        if (this.hasChanged())
        {
            int really=JOptionPane.showConfirmDialog(null,
                    "The current level contains unsaved changes.\nDo you want to proceed?");
            if (really==JOptionPane.NO_OPTION) return false;
        }
        return true;
    }


    public void setChanged(boolean change){
        this.changed=change;
    }

    public boolean hasChanged(){
        return this.changed;
    }

    public ArrayList<AbstractGameObject> getLevel(){
        return level;
    }

    public void setLevel(ArrayList<AbstractGameObject> level){
        this.level=level;
    }

    public void addObject(AbstractGameObject goa){
        BaseController.undoModel.addUndoElement();
        level.add(goa);
        this.setChanged(true);
        view.addPanel(goa);
        view.repaintAll();
    }


    public void removeObject(AbstractGameObject object){
    	BaseController.undoModel.addUndoElement();
       for (int i=0; i<events.size(); i++)
      {
        GameEvent thisEvent=events.get(i);

       //the object is removed if a part of this event
        if (thisEvent.getObjectList().contains(object))
            {thisEvent.getObjectList().remove(object);
             view.repaintAllEvents();
        }
       //case the removed object is the main or last object of the event,
       //the event is removed.

        else if ((object.equals(thisEvent.getFirstObject())) ||
          (thisEvent.getObjectList().size()==1 && thisEvent.getObjectList().contains(object)))
          {
               removeEvent(thisEvent);
               break;
               }
       }
       if (BaseController.selectionModel.isSelected(object))
    	   BaseController.selectionModel.deselect(object);
       level.remove(object);
       view.removePanel(object);
       view.repaintPanel(object);
       view.repaintAll();
       this.setChanged(true);
   }

    public void clearLevel() {
    	BaseController.undoModel.addUndoElement();
        this.level=new ArrayList<AbstractGameObject>();
        this.events=new ArrayList<GameEvent>();
        BaseController.selectionModel.getSelected().removeAll(BaseController.selectionModel.getSelected());
        this.name="(Untitled)";
        this.background=null;
        BaseController.fileHandler.setPath("");
        view.setLevelName(this.name);
        view.getFrame().setTitle("FoA Level Editor - [Untitled]");
        view.removeAllPanels();
        view.removeAllEvents();
        view.paintCurrentLevel();
        this.setChanged(false);
    }

    public void setName(String name){
    	BaseController.undoModel.addUndoElement();
        this.name=name;
        this.setChanged(true);
    }

    public String getName(){
        return this.name;
    }

    public boolean containsPlayer(){
        for (int i=0; i<level.size(); i++)
        {
            if (level.get(i).getName().contains("Player"))
            {
                return true;
            }
        }
        return false;
    }

    public void removeBackground(){
        this.background=null;
        this.setChanged(true);
        view.repaintAll();
    }

    public void setBackground(AbstractGameObject background){
    	BaseController.undoModel.addUndoElement();
        this.background=background;
        this.setChanged(true);
        view.repaintAll();
    }

    public AbstractGameObject getBackground(){
        return this.background;
    }


    public void addEvent(GameEvent event){
    	BaseController.undoModel.addUndoElement();
        events.add(event);
        view.repaintAllEvents();
    }

    public void removeEvent(GameEvent event){
    	BaseController.undoModel.addUndoElement();
        events.remove(event);
        view.repaintAllEvents();
    }

    public ArrayList<GameEvent> getEvents() {
        return this.events;
    }

    public void setEvents(ArrayList<GameEvent> parsedEvents) {
        this.events=parsedEvents;
    }

    public void setView(FoaLvlEditorView aThis) {
        view=aThis;
    }

    public FoaLvlEditorView getView() {
        return view;
    }

    public void setBitmap(AbstractGameObject obj, Bitmap bitmap) {
        obj.setBitmap(bitmap.getBitmapCode());
    }

}
